<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="流域" prop="status">
        <el-select v-model="queryParams.riverBasinName" placeholder="请选择流域" clearable>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->
    <el-row :gutter="10" class="mb8" v-if="tableShow">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain 
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="handleUpdate"
        >修改</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="handleDelete()"
        >删除</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-setting"
          size="mini"
          @click="targetSet"
        >指标修改</el-button>
      </el-col> -->
      <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-row>
    <el-table  :data="busimessList" style="width: 100%"  v-if="refreshTable" v-loading="loading" @selection-change="handleSelectionChange">
        <el-table-column  type="selection" width="55" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column  prop="businesscategory" label="业务类别名称" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column  prop="businesscategorycode" label="业务类别编码" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column  prop="waterbody" label="水体类型" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <template v-for="item in dict.type.sys_bussinesswaterbody">
              <span v-if="scope.row.waterbody === item.value">{{item.label}}</span>
            </template>
          </template>
        </el-table-column>
        <el-table-column  prop="lifecycle" label="使用状况" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column  prop="sortcode" label="排序字段" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >修改</el-button>
            <!-- <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</el-button> -->
            <el-button
              type="text"
              icon="el-icon-setting"
              size="mini"
              @click="targetSet(scope.row)"
            >指标修改</el-button>

          </template>
        </el-table-column>
    </el-table>
     <el-pagination layout="total,sizes,prev, pager, next" :total="this.total" background :page-size="10"  :page-sizes="[10, 20, 50]"  @size-change="handleSizeChange" @current-change="handleCurrentChange"></el-pagination>
    <el-dialog :title="title" :visible.sync="open" :width="targetShow?'80%':'1000px'" append-to-body>
      <target v-if="targetShow" :businesscategorycode="form.businesscategorycode"/>
      <el-form v-else ref="form" :model="form"label-width="200px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="业务类型名称" prop="businesscategory">
              <el-input v-model="form.businesscategory" placeholder="请输入业务类型名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="业务类别编码" prop="businesscategorycode">
              <el-input v-model="form.businesscategorycode" placeholder="请输入业务类别编码"></el-input>
              <!-- <el-select v-model="form.businesscategorycode" placeholder="请选择业务类别编码" style="width: 100%">
                <el-option
                  v-for="dict in businessItemList"
                  :key="dict.id"
                  :label="dict.bussinesscategorycode"
                  :value="dict.bussinesscategorycode"
                ></el-option>
              </el-select> -->
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="父级业务类别编码" prop="fbusinesscategorycode">
              <el-input v-model="form.fbusinesscategorycode" placeholder="父级业务类别编码"></el-input>
              <!-- <el-select v-model="form.fbusinesscategorycode" placeholder="请选择父级业务类别编码" style="width: 100%">
                <el-option
                  v-for="dict in businessItemList"
                  :key="dict.id"
                  :label="dict.bussinesscategorycode"
                  :value="dict.bussinesscategorycode"
                ></el-option>
              </el-select> -->
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="监测分类" prop="monitortype">
              <el-select v-model="form.monitortype" placeholder="请选择监测分类" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.monitor_type_list"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="水体类型" prop="waterbody">
              <el-select v-model="form.waterbody" placeholder="请选择水体类型" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.sys_bussinesswaterbody"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="使用状况" prop="lifecycle">
              <el-radio v-model="form.lifecycle" label="1">是</el-radio>
              <el-radio v-model="form.lifecycle" label="0">否</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否手工监测" prop="ismanual">
              <el-radio v-model="form.ismanual" label="1">是</el-radio>
              <el-radio v-model="form.ismanual" label="0">否</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否自动站" prop="isauto">
              <el-radio v-model="form.isauto" label="1">是</el-radio>
              <el-radio v-model="form.isauto" label="0">否</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否水源地" prop="isdrink">
              <el-radio v-model="form.isdrink" label="1">是</el-radio>
              <el-radio v-model="form.isdrink" label="0">否</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序字段" prop="sortcode">
              <el-input v-model="form.sortcode" placeholder="请输入排序字段" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getBussinesscategoryList ,bussinesscategoryAdd,bussinesscategoryEdit,getBussinessitemsList} from '@/api/dataResources/monitorData/dmTos.js';
import target from './target.vue'
export default {
  dicts:[
    "monitor_type_list",
    "sys_bussinesswaterbody"
  ],
  components:{
    target
  },
  data(){
    return {
      showSearch: true,
      queryParams:{
        // riverBasinName:'',
        pageNum: 1, 
        pageSize: 10,
      },
      tableShow:true,
      busimessList:[],
      businessItemList:[],
      tableSelect:[],
      refreshTable:true,
      loading: false,
      title:'',
      open:false,
      targetShow:false,
      form: {},
      total:0,
    }
  },
  created() {
    // this.getbusinessItemListFn()
    this.getList();
  },

  methods: {
    goback(){
      this.tableShow = true;
    },
    async getbusinessItemListFn(){
      this.businessItemList = [];
      let res = await getBussinessitemsList();
      if(res.code ==200) {
        this.businessItemList = res.rows;
      }
    },
    handleSizeChange(val){
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) { 
      this.queryParams.pageNum = val;
      this.getList();
    },
    handleQuery() {
      this.getList();
    },
    handleAdd() {
      this.open = true;
      this.targetShow = false;
      this.title = "添加";
      this.form = {
        "businesscategorycode": "",
        "businesscategory": "",
        "waterbody": "",
        "algorithm": "",
        "isdrink": "",
        "regionlevel": null,
        "lifecycle": "",
        "fbusinesscategorycode": null,
        "monitortype": "",
        "ismanual": "",
        "isauto": "",
        "imagesurl": "",
        "sortcode": "",
        "priority": "",
        "creatername": null,
        "createrid": null,
        "createtime": null
      }; 
    },
    handleUpdate(row) {
      this.open = true;
      this.targetShow = false;
      this.title = "编辑";
      this.form = {...row};

    },
    async handleDelete(row = null) {
      let id;
      if(row){
        id = row.id;
      } else if(this.tableSelect.length > 0){
        id = this.tableSelect.map(item => item.id).join(',');
      };
      this.getList();
    },
    targetSet(row){
      // this.tableShow = false;
      this.title = "指标修改";
      this.form = {...row};
      this.open = true;
      this.targetShow = true;
    },
    handleSelectionChange(val){
      this.tableSelect = val;
    },
    handleExport() {

    },
    resetQuery() {
      this.queryParams = {
        pageNum: 1, 
        pageSize: 10,
      };
      this.getList();
    },
    submitForm(){
      this.$refs["form"].validate((valid) =>{
        if(valid){
          this.saveByApi();
        }
      })
    }, 
    async saveByApi(){
      this.open = false;
      if(this.form.id) {
        await bussinesscategoryEdit(this.form)
      } else {
        await bussinesscategoryAdd(this.form)
      }
      await this.getList();
    },
    cancel() {
      this.open = false;
      this.targetShow = false;
    },
    async getList() {
      this.tableSelect = [];
      this.loading = true
      let res = await getBussinesscategoryList(this.queryParams);
      if(res.code =='200') {
        this.busimessList = res.rows;
        this.total = res.total;
      }
      this.loading = false;
    },
  },
};
</script>

<style></style>
